﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using System.Drawing;
using System.Windows.Forms;
using fideeraSoft.Utils;
using fideeraSoft.Entities;

namespace fideeraSoft.Paneles
{
    class ProductoPanelBaja:PanelGeneral
    {
        private System.Windows.Forms.Button btEliminar;
        private System.Windows.Forms.DataGridView dgvBaja;
        private System.Windows.Forms.Button btVolver;
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        
        private TableLayoutPanel tlpBuscarProduct;
        private Button btBuscarProduct;
        private TextBox tbBuscarProduct;
        
        private int posicionTablaBotones;
        private TableLayoutPanel tlpBuscarMarca;
        private Button btMarca;
        private TextBox tbMarca;
        private int posicionDataGrid;
        private int posicionTablaMarca=0;

        public ProductoPanelBaja(Handler handler)
        {
            this.formatearPanelGeneral(handler);

            InitializeComponent();

            this.formatearTableTitulo("BAJA DE PRODUCTO");


            this.formatearTablaTLP();

            formatearTablaBuscarPorMarca();

            this.formatearDataGrid();

            this.formatearTablaBotones();


            this.Controls.Add(tlpBuscarProduct);

            this.Controls.Add(tlpBuscarMarca);

            this.Controls.Add(dgvBaja);

            this.Controls.Add(tlpBotones);

        }

        private void formatearTablaTLP()
        {
            tlpBuscarProduct.Controls.Add(btBuscarProduct);
            tlpBuscarProduct.Controls.Add(tbBuscarProduct);
                        
            tlpBuscarProduct.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBuscarProduct.Size.Width), this.InitialRealPointVertical);
            
        }

        private void formatearTablaBuscarPorMarca()
        {
            posicionTablaMarca = tlpBuscarProduct.Size.Height + this.InitialRealPointVertical;

            tlpBuscarMarca.Controls.Add(btMarca);
            tlpBuscarMarca.Controls.Add(tbMarca);

            tlpBuscarMarca.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBuscarMarca.Size.Width), this.posicionTablaMarca);

        }


        private void formatearDataGrid()
        {

            posicionDataGrid = posicionTablaMarca + tlpBuscarMarca.Size.Height + 60;

            dgvBaja.AutoGenerateColumns = true;
            dgvBaja.Size = new Size(this.Size.Width - 100, 100);
            dgvBaja.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvBaja.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvBaja.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvBaja.BorderStyle = BorderStyle.Fixed3D;

            dgvBaja.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvBaja.Size.Width), posicionDataGrid);
        }

        private void formatearTablaBotones()
        {
            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btEliminar);

            posicionTablaBotones = posicionDataGrid + dgvBaja.Size.Height + 40;
            
            //tlpBotones.Size = new Size(400, tbBuscarProvCuil.Size.Height + 9);
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBotones.Size.Width), posicionTablaBotones);

        }

        #region INICIALIZAR COMPONENTES
        private void InitializeComponent()
        {
            this.btEliminar = new System.Windows.Forms.Button();
            this.dgvBaja = new System.Windows.Forms.DataGridView();
            this.btVolver = new System.Windows.Forms.Button();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.tlpBuscarProduct = new System.Windows.Forms.TableLayoutPanel();
            this.btBuscarProduct = new System.Windows.Forms.Button();
            this.tbBuscarProduct = new System.Windows.Forms.TextBox();
            this.tlpBuscarMarca = new System.Windows.Forms.TableLayoutPanel();
            this.btMarca = new System.Windows.Forms.Button();
            this.tbMarca = new System.Windows.Forms.TextBox();
            ((System.ComponentModel.ISupportInitialize)(this.dgvBaja)).BeginInit();
            this.SuspendLayout();
            // 
            // btEliminar
            // 
            this.btEliminar.BackColor = System.Drawing.Color.AliceBlue;
            this.btEliminar.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btEliminar.Location = new System.Drawing.Point(0, 0);
            this.btEliminar.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btEliminar.Name = "btEliminar";
            this.btEliminar.Size = new System.Drawing.Size(75, 40);
            this.btEliminar.TabIndex = 0;
            this.btEliminar.Text = "Eliminar";
            this.btEliminar.UseVisualStyleBackColor = false;
            this.btEliminar.Click += new System.EventHandler(this.btEliminar_Click);
            // 
            // dgvBaja
            // 
            this.dgvBaja.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvBaja.Location = new System.Drawing.Point(0, 0);
            this.dgvBaja.Name = "dgvBaja";
            this.dgvBaja.ReadOnly = true;
            this.dgvBaja.Size = new System.Drawing.Size(240, 150);
            this.dgvBaja.TabIndex = 0;
            this.dgvBaja.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvBaja_CellClick);
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // tlpBuscarProduct
            // 
            this.tlpBuscarProduct.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarProduct.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarProduct.ColumnCount = 2;
            this.tlpBuscarProduct.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProduct.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProduct.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarProduct.Name = "tlpBuscarProduct";
            this.tlpBuscarProduct.RowCount = 1;
            this.tlpBuscarProduct.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProduct.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProduct.Size = new System.Drawing.Size(400, 40);
            this.tlpBuscarProduct.TabIndex = 0;
            // 
            // btBuscarProduct
            // 
            this.btBuscarProduct.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProduct.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProduct.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProduct.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProduct.Name = "btBuscarProduct";
            this.btBuscarProduct.Size = new System.Drawing.Size(75, 23);
            this.btBuscarProduct.TabIndex = 0;
            this.btBuscarProduct.Text = "Buscar Productos (Nombre)";
            this.btBuscarProduct.UseVisualStyleBackColor = false;
            this.btBuscarProduct.Click += new System.EventHandler(this.btBuscarProduct_Click);
            // 
            // tbBuscarProduct
            // 
            this.tbBuscarProduct.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarProduct.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarProduct.MaxLength = 50;
            this.tbBuscarProduct.Name = "tbBuscarProduct";
            this.tbBuscarProduct.Size = new System.Drawing.Size(200, 30);
            this.tbBuscarProduct.TabIndex = 0;
            // 
            // tlpBuscarMarca
            // 
            this.tlpBuscarMarca.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarMarca.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarMarca.ColumnCount = 2;
            this.tlpBuscarMarca.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMarca.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMarca.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarMarca.Name = "tlpBuscarMarca";
            this.tlpBuscarMarca.RowCount = 1;
            this.tlpBuscarMarca.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMarca.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarMarca.Size = new System.Drawing.Size(400, 40);
            this.tlpBuscarMarca.TabIndex = 0;
            // 
            // btMarca
            // 
            this.btMarca.BackColor = System.Drawing.Color.AliceBlue;
            this.btMarca.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btMarca.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btMarca.Location = new System.Drawing.Point(0, 0);
            this.btMarca.Name = "btMarca";
            this.btMarca.Size = new System.Drawing.Size(75, 23);
            this.btMarca.TabIndex = 0;
            this.btMarca.Text = "Buscar Productos (Marca)";
            this.btMarca.UseVisualStyleBackColor = false;
            this.btMarca.Click += new System.EventHandler(this.btMarca_Click);
            // 
            // tbMarca
            // 
            this.tbMarca.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbMarca.Location = new System.Drawing.Point(0, 0);
            this.tbMarca.MaxLength = 50;
            this.tbMarca.Name = "tbMarca";
            this.tbMarca.Size = new System.Drawing.Size(200, 30);
            this.tbMarca.TabIndex = 0;
            ((System.ComponentModel.ISupportInitialize)(this.dgvBaja)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion


        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void btEliminar_Click(object sender, EventArgs e)
        {
            try
            {
                if (dgvBaja.CurrentCell == null)
                {
                    MessageBox.Show("NO HAY DATOS EN LA TABLA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if ((dgvBaja != null) && (dgvBaja.Rows.Count != 0))
                {
                    int indx = dgvBaja.CurrentCell.RowIndex;

                    string strDataId = dgvBaja[0, indx].Value.ToString();
                    string strDataName = dgvBaja[1, indx].Value.ToString();
                    
                    string strMarca;
                    if (dgvBaja[5, indx].Value != null) strMarca = dgvBaja[5, indx].Value.ToString(); else strMarca = "";

                    DialogResult res = MessageBox.Show("Seguro quiere eliminar al Producto: \n Nombre: " + strDataName + "\n ID: " + strDataId+"\n Marca: "+strMarca, "INFO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (res == DialogResult.Yes)
                    {
                        this.handler.eliminarProductoPorID(Convert.ToInt32(strDataId));
                        MessageBox.Show("Producto eliminado correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        tbBuscarProduct.Clear();                        

                        if (this.dgvBaja.DataSource != null)
                        {
                            this.dgvBaja.DataSource = null;
                        }
                        else
                        {
                            //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[e.RowIndex]);
                            //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[0]);
                            this.dgvBaja.Rows.Clear();
                        }
                        this.dgvBaja.Refresh();
                    }
                    else
                    {

                    }
                }
                else
                {
                    MessageBox.Show("No hay ningún Producto en la lista", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (MiException exc)
            {
                MessageBox.Show("No se pudo eliminar Producto. Probablemente el producto este asociado a una o más producciones.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                CLogger.Append("[btEliminar_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
            }
        }

        private void btBuscarProduct_Click(object sender, EventArgs e)
        {

            if (dgvBaja.CurrentCell == null)
            {
                //MessageBox.Show("NO HAY DATOS EN LA TABLA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                //return;
            }


            dgvBaja.ClearSelection();
            if (this.dgvBaja.DataSource != null)
            {
                this.dgvBaja.DataSource = null;
            }
            else
            {
                //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[e.RowIndex]);
                //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[0]);
                this.dgvBaja.Rows.Clear();
            }
            dgvBaja.Refresh();

            if (!string.IsNullOrEmpty(tbBuscarProduct.Text))
            {                
                try
                {
                    List<Producto> lstProductos = handler.buscarListaProductosPorNombre(tbBuscarProduct.Text);
                    
                    //MessageBox.Show("PRoducto Encontrado", "DEBUG", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);  //DEBUG

                    if (lstProductos != null)
                    {                        
                        dgvBaja.DataSource = lstProductos;
                        //   MessageBox.Show("CARGA DE PROVEEDOR ENCONTRADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //DEBUG
                        dgvBaja.EndEdit();
                        dgvBaja.Refresh();
                      //  MessageBox.Show("PRoducto Encontrado", "DEBUG", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);  //DEBUG
                    }
                    else
                    {
                        MessageBox.Show("NO SE ENCONTRARON PRODUCTOS CON ESE NOMBRE", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (MiException exc)
                {
                    //MessageBox.Show(exc.Message);     //DEBUG
                    CLogger.Append("[btBuscarProduct_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
                tbBuscarProduct.Clear();
            }
            else
            {
                MessageBox.Show("NO SE INGRESO NINGÚN NOMBRE \nÓ LA CANTIDAD DE DIGITOS ES INCORRECTA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void dgvBaja_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvBaja.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void btMarca_Click(object sender, EventArgs e)
        {

            if (dgvBaja.CurrentCell == null)
            {
              //  MessageBox.Show("NO HAY DATOS EN LA TABLA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
               // return;
            }            
            if (this.dgvBaja.DataSource != null)
            {
                this.dgvBaja.DataSource = null;
                dgvBaja.ClearSelection();
            }
            else
            {
                //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[e.RowIndex]);
                //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[0]);
                this.dgvBaja.Rows.Clear();
            }
            dgvBaja.Refresh();

            if (!string.IsNullOrEmpty(tbMarca.Text))
            {
                try
                {
                    List<Producto> lstProductos = handler.buscarListaProductosPorMarca(tbMarca.Text);

                    //MessageBox.Show("PRoducto Encontrado", "DEBUG", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);  //DEBUG

                    if (lstProductos != null)
                    {
                        dgvBaja.DataSource = lstProductos;
                        //   MessageBox.Show("CARGA DE PROVEEDOR ENCONTRADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //DEBUG
                        dgvBaja.EndEdit();
                        dgvBaja.Refresh();
                        //  MessageBox.Show("PRoducto Encontrado", "DEBUG", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);  //DEBUG
                    }
                    else
                    {
                        MessageBox.Show("NO SE ENCONTRARON PRODUCTOS CON ESA MARCA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (MiException exc)
                {
                    //MessageBox.Show(exc.Message);     //DEBUG
                    CLogger.Append("[btBuscarProduct_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
                
                tbMarca.Clear();
            }
            else
            {
                MessageBox.Show("NO SE INGRESO NINGUNA MARCA \nÓ LA CANTIDAD DE DIGITOS ES INCORRECTA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

    }
}
